package top.forgirl.service;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import top.forgirl.domain.Produce;
import top.forgirl.mapper.ProduceMapper;

import java.util.List;

@Service
public class ProduceService {

    @Autowired
    private ProduceMapper produceMapper;

    public List<Produce> list(String cId, String name, String comment) {

        if ("EOF".equals(cId)) {
            cId = "";
        }
        if ("EOF".equals(name)) {
            name = "";
        }
        if ("EOF".equals(comment)) {
            comment = "";
        }

        comment = comment.replaceAll(" ", "%");

        return produceMapper.list(cId, name, comment);
    }

    public Produce delete(String id) {
        Produce produce = produceMapper.selectOne(id);
        if (produce != null) {
            int result = produceMapper.delete(id);
            if (result > 0) {
                return produce;
            } else {
                return null;
            }
        } else {
            return null;
        }
    }

    public Produce insert(Produce produce) {

        int result = produceMapper.insert(produce);
        if (result>0) {
            return produce;
        }else {
            return null;
        }
    }
}
